/*
 * USART_UI.c
 *
 *  Created on: 08.01.2011
 *      Author: Tavin
 */
#include <stdint.h>


#include "conf_usb.h"
#include "usb_descriptors.h"
#include "usb_standard_request.h"

#include "uart_usb_lib.h"
#include "usb_drv.h"
#include "console.h"

#include "GamePad.h"


static volatile U16  sof_cnt;



volatile int c;

void Console_init()
{

  sof_cnt   =0 ;
  uart_usb_init(&uart_usb_console, RX_EP, TX_EP);
  uart_usb_init(&uart_usb_upload, RX_2_EP, TX_2_EP);

#ifndef FREERTOS_USED
  #if USB_HOST_FEATURE == ENABLED
  // If both device and host features are enabled, check if device mode is engaged
  // (accessing the USB registers of a non-engaged mode, even with load operations,
  // may corrupt USB FIFO data).
  if (Is_usb_device())
  #endif  // USB_HOST_FEATURE == ENABLED
    Usb_enable_sof_interrupt();
#endif  // FREERTOS_USED


c=0;

}




//!
//! @brief Entry point of the device CDC task management
//!

void Console_do(void)
{



    // First, check the device enumeration state
    if (!Is_device_enumerated()) return;



    if( sof_cnt>=NB_MS_BEFORE_FLUSH )  //Flush buffer in Timeout
    {
       sof_cnt=0;
       uart_usb_flush(&uart_usb_console);
       uart_usb_flush(&uart_usb_upload);
    }


}


void usb_sof_action(void)
{
  sof_cnt++;
}



void Console_putChar(uint16_t myChar)
{

	if(uart_usb_tx_ready(&uart_usb_console)){
		uart_usb_putchar(&uart_usb_console, myChar);  // send over usb
		c++;
	}
}



/**
 * @author Dominik Ollesz
 * @date 20.04.2011
 */
void Console_putCharWait(uint16_t myChar) {
	while(!uart_usb_tx_ready(&uart_usb_console)){
		uart_usb_putchar(&uart_usb_console, myChar);  // send over usb
		c++;
	}
}

void Console_putStringWait(char *string) {
	char* s = string;
	while (*s != '\0') Console_putCharWait((uint16_t)(*s++));
}

